﻿/*! @file
********************************************************************************
<PRE>
文 件 名    : PlugInSectionHandler.cs
说    明    : 处理配置文件中的插件配置
作    者    : 汪泽然
--------------------------------------------------------------------------------
备    注    : 
--------------------------------------------------------------------------------
修改记录: 
 * 日   期      版  本       修改人      修改内容 
 * 2009-7-11    0.5.0       汪泽然       实现对插件的检测功能
 *                                       更改了配置文件格式，将插件放在PlugIns目录下
 * 2009-7-19    0.5.2       汪泽然       取消了对插件的统一注册，改为单个注册
</PRE>
********************************************************************************
* 版权所有(c) 2008 ~ 2009, 汪泽然, 保留所有权利
*******************************************************************************/
using System;
using System.Xml;
using System.Reflection;
using System.Collections;
using System.Configuration;
using System.Collections.Generic;
using Wangzeran.Transform.Core.PlugIn;
using Wangzeran.Transform.Core.Support;
using Wangzeran.Transform.Core.ITransform;

namespace Wangzeran.Transform.Core.Cfg
{
    /// <summary>
    /// 处理配置文件中的PlugIns插件节点
    /// </summary>
    public class PlugInsSectionHandler : IConfigurationSectionHandler
    {
        /// <summary>
        /// 插件路径
        /// </summary>
        private const string PlugInsPath = @"PlugIns\";
        /// <summary>
        /// 插件类库的扩展名
        /// </summary>
        private const string PlugInExt = ".dll";
        /// <summary>
        /// 插件完全限定名 属性标签
        /// </summary>
        private const string FactoryAttribute = "factory";
        /// <summary>
        /// 程序集名称 属性标签
        /// </summary>
        private const string AssemblyAttribute = "assembly";

        /// <summary>
        /// 获取插件配置信息
        /// 返回IDictionary(string, BaseTransformFactory)集合
        /// </summary>
        /// <param name="parent"></param>
        /// <param name="configContext"></param>
        /// <param name="section"></param>
        /// <returns></returns>
        public object Create(object parent, object configContext, XmlNode section)
        {
            // 插件集合
            IDictionary<string, BaseTransformFactory> plugIns = 
                new Dictionary<string, BaseTransformFactory>();

            // 获取PlugIn节点的Type属性，并实例化为对象
            foreach (XmlNode node in section.ChildNodes)
            {
                try
                {
                    string assemblyName = node.Attributes[AssemblyAttribute].Value;
                    string typeName = node.Attributes[FactoryAttribute].Value;

                    Assembly assembly = Assembly.LoadFile(GetPlugInPath(assemblyName));
                    if (assembly == null)
                    {
                        continue;
                    }

                    BaseTransformFactory factory =
                        (BaseTransformFactory)assembly.CreateInstance(typeName);

                    if (factory.IsInitFactory())
                    {
                        plugIns.Add(factory.TargetIDEVersion, factory);
                    }
                }
                catch
                {
                    continue;
                }
            }

            return plugIns;
        }

        /// <summary>
        /// 获取插件DLL的绝对路径
        /// </summary>
        /// <param name="assemblyName">程序集名称</param>
        /// <returns></returns>
        private string GetPlugInPath(string assemblyName)
        {
            string path = Assembly.GetExecutingAssembly().Location;
            path = path.Remove(path.LastIndexOf(@"\") + 1);
            return path + PlugInsPath + assemblyName + PlugInExt;
        }

    }
}
